﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class CityService : BaseService<City>, ICityService
    {
        public List<CityDTO> GetCities()
        {
            return GetAll().ToList().Select(c => ToDTO(c)).ToList();
        }
        public CityDTO GetDTOById(long id)
        {
            return ToDTO(GetById(id));
        }

        private CityDTO ToDTO(City entity)
        {
            CityDTO dto = new CityDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.Name = entity.Name;
            return dto;
        }

        public void Add(string name)
        {
            City entity = new City();
            entity.Name = name;
            Add(entity);
        }

        public void Update(long id, string name)
        {
            City entity = GetById(id);
            if (entity==null)
            {
                throw new Exception("获取不到城市(id:"+id+")信息");
            }
            entity.Name = name;
            Update(entity);
        }
    }
}
